// pages/my/index.js
const { inspectorLogout } = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topTitle: {
      top: 0,
      height: 0,
    },
    name: '',
    phone: '',
    actionSheetShow: false,
    actions: [
      {
        key: 'logout',
        name: '退出',
      },
    ],
  },
  goInspector() {
    const inspector = wx.getStorageSync('inspector-info')
    if (inspector) {
      try {
        const { token } = JSON.parse(inspector)
        if (token) {
          wx.navigateTo({
            url: '/pages/report/index',
          })
          return
        }
      } catch (e) { }
    }
    wx.navigateTo({
      url: '/pages/inspectorLogin/index',
    })
  },
  goPraise() {
    wx.navigateTo({
      url: '/pages/praise/index',
    })
  },
  goUserLogin() {
    wx.navigateTo({
      url: '/pages/userLogin/index',
    })
  },
  goJoinUs() {
    wx.navigateTo({
      url: '/pages/join/index',
    })
  },
  goServiceLogin() {
    const service = wx.getStorageSync('service-info')
    if (service) {
      try {
        const { token } = JSON.parse(service)
        if (token) {
          wx.navigateTo({
            url: '/pages/serviceOrder/index',
          })
          return
        }
      } catch (e) { }
    }
    wx.navigateTo({
      url: '/pages/serviceLogin/index',
    })
  },
  handleAccount() {
    this.setData({
      actionSheetShow: true
    })
  },
  handleSheetCancel() {
    this.setData({
      actionSheetShow: false
    })
  },
  getInspectorInfo() {
    const inspector = wx.getStorageSync('inspector-info')
    if (inspector) {
      const { name, phone } = JSON.parse(inspector)
      this.setData({
        name,
        phone
      })
    } else {
      this.setData({
        name: '',
        phone: ''
      })
    }
  },
  async handleSheetSelect(e) {
    if (e.detail.key === 'logout') {
      const inspector = wx.getStorageSync('inspector-info')
      if (inspector) {
        const { token: Authorization } = JSON.parse(inspector)
        const res = await inspectorLogout({}, {
          header: { Authorization }
        })
        wx.showToast({
          title: '退出成功',
          icon: 'none'
        })
        wx.removeStorageSync('inspector-info')
        this.getInspectorInfo();
        this.handleSheetCancel()
      }
    }
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    const {
      top,
      height
    } = wx.getMenuButtonBoundingClientRect();
    this.setData({
      topTitle: {
        top,
        height
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
    this.getInspectorInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})